package org.csu.mypetstore.service.impl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.csu.mypetstore.domain.LineItem;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.domain.OrderStatus;
import org.csu.mypetstore.persistence.OrderMapper;
import org.csu.mypetstore.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Order getOrder(int orderID) {
        Order order = orderMapper.getOrder(orderID);
        order.setOrderStatus(orderMapper.getOrderStatus(orderID));
        order.setLineItems(orderMapper.getOrderLineItem(orderID));
        return order;
    }

    @Override
    public String getStatus(int orderID) {
        return orderMapper.getStatus(orderID);
    }

    @Override
    public List<Order> getOrderListByUserID(String userID) {
        List<Order> orders = orderMapper.getOrderListByUserID(userID);
        for (Order order:orders) {
            int orderID = order.getOrderId();
            order.setOrderStatus(orderMapper.getOrderStatus(orderID));
            order.setLineItems(orderMapper.getOrderLineItem(orderID));
        }
        System.out.println("order size:" + orders.size());
        return orders;
    }

    @Override
    public List<Order> getAllOrders() {
        List<Order> orders = orderMapper.getAllOrders();
        for (Order order:orders) {
            int orderID = order.getOrderId();
            order.setOrderStatus(orderMapper.getOrderStatus(orderID));
            order.setLineItems(orderMapper.getOrderLineItem(orderID));
        }
        return orders;
    }

    @Override
    public void addOrder(Order order) {
        orderMapper.addOrder(order);
        for (LineItem lineItem : order.getLineItems())
            orderMapper.addLineItem(lineItem);
        for (OrderStatus orderStatus : order.getOrderStatus())
            orderMapper.addOrderStatus(orderStatus);
    }

    @Override
    public void updateOrder(Order order) {
        orderMapper.updateOrder(order);
    }

    @Override
    public void updateOrderStatus(OrderStatus orderStatus) {
        orderMapper.updateOrderStatus(orderStatus);
    }

    @Override
    public void deleteOrder(int orderID){
        orderMapper.deleteOrder(orderID);
    }

    @Override
    public JSONObject setJSON(Order order){

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("orderId",order.getOrderId());
        jsonObject.put("username",order.getUsername());
        jsonObject.put("orderDate",(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(order.getOrderDate()));
        jsonObject.put("shipAddress1",order.getShipAddress1());
        jsonObject.put("shipAddress2",order.getShipAddress2());
        jsonObject.put("shipCity",order.getShipCity());
        jsonObject.put("shipState",order.getShipState());
        jsonObject.put("shipZip",order.getShipZip());
        jsonObject.put("shipCountry",order.getShipCountry());
        jsonObject.put("billAddress1",order.getBillAddress1());
        jsonObject.put("billAddress2",order.getBillAddress2());
        jsonObject.put("billCity",order.getBillCity());
        jsonObject.put("billState",order.getBillState());
        jsonObject.put("billZip",order.getBillZip());
        jsonObject.put("billCountry",order.getBillCountry());
        jsonObject.put("courier",order.getCourier());
        jsonObject.put("totalPrice",order.getTotalPrice());
        jsonObject.put("billToFirstName",order.getBillToFirstName());
        jsonObject.put("billToLastName",order.getBillToLastName());
        jsonObject.put("shipToFirstName",order.getShipToFirstName());
        jsonObject.put("shipToLastName",order.getShipToLastName());
        jsonObject.put("creditCard",order.getCreditCard());
        jsonObject.put("expiryDate",order.getExpiryDate());
        jsonObject.put("cardType",order.getCardType());
        jsonObject.put("locale",order.getLocale());
        JSONArray jsonArray = new JSONArray();
        for(int i=0;i<order.getLineItems().size();i++) {
            LineItem lineItem = order.getLineItems().get(i);
            JSONObject object = new JSONObject();
            object.put("orderId",lineItem.getOrderId());
            object.put("lineNumber",lineItem.getLineNumber());
            object.put("itemId", lineItem.getItemId());
            object.put("quantity", lineItem.getQuantity());
            object.put("unitPrice", lineItem.getUnitPrice());
            jsonArray.add(object);
        }
        jsonObject.put("lineItems",jsonArray);

        JSONArray jsonArray1 = new JSONArray();
        for(int i=0;i<order.getOrderStatus().size();i++) {
            OrderStatus orderStatus = order.getOrderStatus().get(i);
            JSONObject object = new JSONObject();
            object.put("orderId",orderStatus.getOrderId());
            object.put("lineNumber",orderStatus.getLineNumber());
            object.put("timestamp", orderStatus.getTimestamp().toString());
            object.put("status", orderStatus.getStatus());
            jsonArray1.add(object);
        }
        jsonObject.put("orderStatus",jsonArray1);

        return jsonObject;
    }
}
